<?php
class SqlSrv_SP_Driver
{
	protected $stmt;
	protected $result;
	protected $params;
	private $_conn;
	
	public function __construct()
	{
		$this->_conn = get_instance()->db->conn_id;
	}
	
	function prepare($sp_name, $params = array())
	{	
		$this->stmt = sqlsrv_prepare($this->_db, "{call {$sp_name}}");
		$this->params = array();
		return $this->stmt;
	}
	
	function execute()
	{
		return sqlsrv_execute($this->stmt);
	}	

	function result($sp_name, $params = array(), $assoc = true)
	{
		$stmt = sqlsrv_query($this->_conn, "{call {$sp_name}}", $params);
		$rows = array();
	    if($assoc)
	    {
	        $row = array();
	        while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))
	        {
	            $rows[] = $row;
	        }
	    }
	    sqlsrv_free_stmt($stmt);
	    return $rows;
	}
	
	function displayErrors()
    {
         $errors = sqlsrv_errors(SQLSRV_ERR_ERRORS);
         foreach( $errors as $error )
         {
              echo "Error: ".$error['message']."\n";
         }
    }
    
    function displayWarnings()
    {
         $warnings = sqlsrv_errors(SQLSRV_ERR_WARNINGS);
         if(!is_null($warnings))
         {
              foreach( $warnings as $warning )
              {
                   echo "Warning: ".$warning['message']."\n";
              }
         }
    }
}